package com.tuyrt.firstcode.kotlin.basic

/**
 * Created by tuyrt7 on 2021/4/6.
 * 运算符重载 + 扩展函数
 */

/**
a + b                            a.plus(b)
a - b                            a.minus(b)
a * b                            a.times(b)
a / b                            a.div(b)
a % b                            a.rem(b)
a++                              a.inc()
a--                              a.dec()
+a                               a.unaryPlus()
-a                               a.unaryMinus()
!a                               a.not()
a == b                           a.equals(b)
a > b
a < b
a >= b                           a.compareTo(b)
a <= b
a..b                             a.rangTo(b)
a[b]                             a.get(b)
a[b]=c                           a.set(b,c)
a in b                           b.contains(a)
 */

class Money(val value: Int) {

    operator fun plus(money: Money) = Money(value + money.value)

    operator fun plus(newValue: Int) = Money(this.value + newValue)
}

// 扩展函数 + 运算符
operator fun String.times(times: Int): String {
    val builder = StringBuilder()
    repeat(times) {
        builder.append(this)
    }
    return builder.toString()
}

fun main() {
    val money = Money(1) + Money(5)
    println("money is:${money.value}")

    val hello = "Hello "
    println("hello*2 is: ${hello * 2}")
}
